package com.yang.frost.blueberry.weixin.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yang.frost.blueberry.controller.WxController;
import com.yang.frost.blueberry.mapper.WxAccountMapper;
import com.yang.frost.blueberry.params.WxMpParam;
import com.yang.frost.blueberry.util.HttpRequestUtil;
import com.yang.frost.blueberry.util.Object2MapUtil;
import com.yang.frost.blueberry.util.OkHttpUtil;
import com.yang.frost.blueberry.weixin.config.WxConfig;
import com.yang.frost.blueberry.weixin.service.WxMpService;
import com.yang.frost.blueberry.weixin.service.WxService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 10296
 * @date 2021/1/29
 * @Description: 微信公众号管理类
 */

@Service
public class WxMpServiceImpl implements WxMpService {


    private Logger logger = LoggerFactory.getLogger(WxMpService.class);
    @Autowired
    private WxService wxService;

    @Autowired
    private HttpRequestUtil requestUtil;

    @Autowired
    private WxAccountMapper accountMapper;

    @Override
    public List<Map<String, Object>> userList(String nextOpenid) throws Exception {
        String accessToken = wxService.getAccessToken();
        List<Map<String, Object>> retList = new ArrayList<>();
        //获取列表信息
        String url = String.format(WxConfig.GET_USER_LIST, accessToken,
                nextOpenid == null ? null : "next_openid=" + nextOpenid);
        JSONObject jsonObject = requestUtil.httpsRequest(url, "GET", null);
        logger.info(jsonObject + "");
        JSONObject data = jsonObject.getJSONObject("data");
        String next_openid = jsonObject.getString("next_openid");
        JSONArray openIds = data.getJSONArray("openid");
        for (int i = 0; i < openIds.size(); i++) {
            String openId = openIds.getString(i);
            String batchGetUrl3 = String.format(WxConfig.USER_INFO, accessToken, openId);
            JSONObject obj = requestUtil.httpsRequest(batchGetUrl3, "GET", null);
            Map<String, Object> params = JSONObject.parseObject(obj.toJSONString(), new TypeReference<Map<String, Object>>() {
            });
            JSONArray arr = (JSONArray) params.get("tagid_list");
            if (arr != null && arr.size() > 0) {

                params.put("tagid_list", arr.toJSONString());
            } else {
                params.put("tagid_list", "");

            }
            accountMapper.saveWxAccount(params);
            retList.add(params);

            logger.info(params + "");

        }


        return retList;
    }

    @Override
    public List<Map<String, Object>> qryWxAccount(WxMpParam param) throws Exception {
        Map<String, Object> paramsMap = Object2MapUtil.objectToMap(param);

        List<Map<String, Object>> userList = accountMapper.qryWxAccount(param);
        logger.info(userList + "");
        return userList;
    }


}
